﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace MixNP.Web.Classifieds.Account
{
    public partial class SignIn : MixNP.BusinessLayer.BaseClass
    {
        static Random rnd = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
            int r = rnd.Next(GetTitles().Count);
            TitleLiteral.Text = (string)GetTitles()[r];

            if (!IsPostBack)
            {
                if (Request.IsAuthenticated)
                {
                    Session["UserId"] = User.Identity.Name;
                    Response.Redirect(FormsAuthentication.DefaultUrl, true);
                }
            }

            EmailTextBox.Focus();
        }

        private List<string> GetTitles()
        {
            List<string> list = new List<string>();

            list.Add("Let's Start Here -->");
            list.Add("You Are Back!!!");
            list.Add("We Missed You ...");
            list.Add("Where Had You Gone?");
            list.Add("How have you been?");
            list.Add("How was your day?");
            list.Add("Nice to see you.");
            list.Add("What's up?");
            list.Add("You Are Awesome!");
            list.Add("Welcome!");

            return list;
        }

        protected void SignInButton_Click(object sender, EventArgs e)
        {
            string emailAddress = EmailTextBox.Text;

            if (!MixNP.BusinessLayer.Users.IsValidEmailAddress(emailAddress))
            {
                ErrorLiteral.Text = "Invalid email address. Your IP is now logged.";
                Pes.Utility.PageUtility.InvalidPasswordAttempts(this.Page, 1);
                return;
            }

            string newSalt = Guid.NewGuid().ToString();
            string salt = MixNP.BusinessLayer.Users.GetSaltFromEmailAddress(emailAddress);

            string password = PasswordTextBox.Text;
            string hashedPasword = Pes.Utility.Conversion.HashSha512(password, salt);
            string saltedPassword = Pes.Utility.Conversion.HashSha512(hashedPasword, newSalt);

            if (MixNP.BusinessLayer.Users.AuthenticatePassword(emailAddress, saltedPassword, newSalt))
            {
                Session["UserId"] = emailAddress;
                FormsAuthentication.RedirectFromLoginPage(emailAddress, RememberMeCheckBox.Checked);
            }
            else
            {
                ErrorLiteral.Text = "Access is denied.";
                Pes.Utility.PageUtility.InvalidPasswordAttempts(this.Page, 1);
            }
        }
    }
}